package com.dayu.statistics.service.impl;

import com.dayu.common.utils.SecurityUtils;
import com.dayu.statistics.mapper.DyWorkMapper;
import com.dayu.statistics.service.DyWorkService;
import com.dayu.statistics.vo.WorkNumberVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DyWorkServiceImpl implements DyWorkService {


    @Resource
    private DyWorkMapper dyWorkMapper;

    @Override
    public WorkNumberVo queryWorkNumber() {
        String username = SecurityUtils.getUsername();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        WorkNumberVo vo = dyWorkMapper.queryWorkNumber(username, userId);
        vo.sumTotal();
        return vo;
    }

    @Override
    public List<Map<String, Object>> saleBillList(String date) {
        Date time = new Date();
        if(!ObjectUtils.isEmpty(date)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                time = sdf.parse(date);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        return dyWorkMapper.saleBillWorkList(SecurityUtils.getLoginUser().getUser().getUserId(), time);
    }

    @Override
    public List<Map<String, Object>> offerBillList(String date) {
        Date time = new Date();
        if(!ObjectUtils.isEmpty(date)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                time = sdf.parse(date);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return dyWorkMapper.offerBillWorkList(SecurityUtils.getLoginUser().getUser().getUserId(), time);
    }
}
